package com.example.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class LoginActivity extends AppCompatActivity {
    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button buttonLogin;
    private CheckBox cb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        TextView tv = findViewById(R.id.tv_info);
        // 初始化控件
        cb = findViewById(R.id.checkBox);
        editTextUsername = findViewById(R.id.editTextText);
        editTextPassword = findViewById(R.id.editTextTextPassword);
        buttonLogin = findViewById(R.id.button);
        tv.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
            startActivity(intent);
        });

        // 登录按钮点击事件
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // 获取用户输入的用户名和密码
                String username = editTextUsername.getText().toString().trim();
                String password = editTextPassword.getText().toString().trim();
                SharedPreferences sp = getSharedPreferences("accountpasswd",MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                boolean b = editor.commit();
                if(b){
                    Toast.makeText(LoginActivity.this, "存储成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(LoginActivity.this, "存储失败", Toast.LENGTH_SHORT).show();
                }
                if (cb.isChecked()) {
                    // 保存用户名、密码和记住密码的状态
                    editor.putString("account", username);
                    editor.putString("passwd", password);
                    editor.putBoolean("remember", true);
                } else {
                    // 清除保存的用户名和密码
                    editor.remove("account");
                    editor.remove("passwd");
                    editor.putBoolean("remember", false);
                }
                editor.apply();
                // 简单的登录验证逻辑
                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空！", Toast.LENGTH_SHORT).show();
                }  else {
                    Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
        checkRememberPassword();
    }
    private void checkRememberPassword() {
        SharedPreferences sp = getSharedPreferences("accountpasswd", MODE_PRIVATE);
        if (sp.getBoolean("remember", false)) {
            String account = sp.getString("account", "");
            String passwd = sp.getString("passwd", "");
            editTextUsername.setText(account);
            editTextPassword.setText(passwd);
            cb.setChecked(true);
        }
    }
}